<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;

class Statistic extends AdminBase
{

    public function section()
    {
        $param = $this->request->param();
        $users = Db::name('admin')->alias('a')->join('auth_group_access b','a.id=b.admin_id')->field('a.id,a.username,a.nickname')->where('b.group_id',7)->select()->toArray();
        if(empty($param['start'])) $param['start'] = date('Y-m-d',strtotime('-7 day'));
        if(empty($param['end'])) $param['end'] = date('Y-m-d');
        foreach($users as $k => $v){
            $count_all = Db::name('data_text_section')->where('username',$v['username'])->count();
            $count_all += Db::name('data_text_start')->where('username',$v['username'])->count();
            $count_all += Db::name('data_text_end')->where('username',$v['username'])->count();
            $data_all[$k]['value'] = $count_all;
            $data_all[$k]['name'] = $v['nickname'].'('.$count_all.')';

            $count_today = Db::name('data_text_section')->where('username',$v['username'])->whereDay('create_time')->count();
            $count_today += Db::name('data_text_start')->where('username',$v['username'])->whereDay('create_time')->count();
            $count_today += Db::name('data_text_end')->where('username',$v['username'])->whereDay('create_time')->count();
            $data_today[$k]['value'] = $count_today;
            $data_today[$k]['name'] = $v['nickname'].'('.$count_today.')';

            $count_yesterday = Db::name('data_text_section')->where('username',$v['username'])->whereDay('create_time', 'yesterday')->count();
            $count_yesterday += Db::name('data_text_start')->where('username',$v['username'])->whereDay('create_time', 'yesterday')->count();
            $count_yesterday += Db::name('data_text_end')->where('username',$v['username'])->whereDay('create_time', 'yesterday')->count();
            $data_yesterday[$k]['value'] = $count_yesterday;
            $data_yesterday[$k]['name'] = $v['nickname'].'('.$count_yesterday.')';

            $section = Db::name('data_text_section')->field('DATE_FORMAT(create_time,"%Y-%m-%d") days, count(*) num')->where('username',$v['username'])->whereBetweenTime('create_time', $param['start'], $param['end'])->group('days')->select()->toArray();
            $start = Db::name('data_text_start')->field('DATE_FORMAT(create_time,"%Y-%m-%d") days, count(*) num')->where('username',$v['username'])->whereBetweenTime('create_time', $param['start'], $param['end'])->group('days')->select()->toArray();
            $end = Db::name('data_text_end')->field('DATE_FORMAT(create_time,"%Y-%m-%d") days, count(*) num')->where('username',$v['username'])->whereBetweenTime('create_time', $param['start'], $param['end'])->group('days')->select()->toArray();
            $data[$k]['section'] = $section;
            $data[$k]['start'] = $start;
            $data[$k]['end'] = $end;
            $data[$k]['name'] = $v['nickname'];
            $data_users[] = $v['nickname'];
        }

        $date_list = $this->dateSplit($param['start'],$param['end']);

        foreach($data as $k => $v){
            foreach($v['section'] as $kk => $vv){
                $data[$k]['section'][$vv['days']] = $vv['num'];
                unset($data[$k]['section'][$kk]);
            }
            foreach($v['start'] as $kk => $vv){
                $data[$k]['start'][$vv['days']] = $vv['num'];
                unset($data[$k]['start'][$kk]);
            }
            foreach($v['end'] as $kk => $vv){
                $data[$k]['end'][$vv['days']] = $vv['num'];
                unset($data[$k]['end'][$kk]);
            }
        }

        foreach($data as $k => $v){
            foreach($date_list as $date){
                if(!array_key_exists($date, $v['section'])){
                    $data[$k]['section'][$date] = 0;
                }
                if(!array_key_exists($date, $v['start'])){
                    $data[$k]['start'][$date] = 0;
                }
                if(!array_key_exists($date, $v['end'])){
                    $data[$k]['end'][$date] = 0;
                }
                $data[$k]['value'][$date] = $data[$k]['section'][$date] + $data[$k]['start'][$date] + $data[$k]['end'][$date];
            }
            unset($data[$k]['section']);
            unset($data[$k]['start']);
            unset($data[$k]['end']);
            ksort($data[$k]['value']);
        } 

        $data_all = json_encode($data_all);
        $data_today = json_encode($data_today);
        $data_yesterday = json_encode($data_yesterday);

        $data = json_encode($data,true);
        $data_users = json_encode($data_users);
        $date_list = json_encode($date_list);
        
        return view('',['param'=>$param,'data_all'=>$data_all,'data_today'=>$data_today,'data_yesterday'=>$data_yesterday,'data'=>$data,'data_users'=>$data_users,'date_list'=>$date_list]);
    }

    private function dateSplit($startDate, $endDate){
        $flag = 'day';
        $format = 'Y-m-d';
        if(substr_count($startDate, '-') == 1){
            $flag = 'month';
            $format = 'Y-m';
        }
        $list[] = $startDate;
        while(1){
            $startDate = date($format, strtotime("$startDate +1 $flag"));
            if(strtotime($startDate) >= strtotime($endDate)){
                break;
            }
            $list[] = $startDate;
        }
        return $list;
    }

}
